package org.otherworld.listener;

import java.io.*;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;


import org.otherworld.process.Sys;

import org.otherworld.property.PropertyLoader;
import org.otherworld.command.Command;
import org.otherworld.command.CommandCollector;
import org.otherworld.command.Display;
import org.otherworld.command.List;
import org.otherworld.interpreter.Interpreter;

class Listener {

	PropertyLoader loader;
	Hashtable<String, Object> messages;

	public Listener() throws IOException, FileNotFoundException {
		loader = new PropertyLoader();
		messages = loader.getProperties();
	}
	
	
	
	
	
	public void init() {

		if(Sys.initializeSystem()) {
			
		}
		
		try {
			CommandCollector commandCollector = new CommandCollector();
			Hashtable<String,Command> commands = commandCollector.getCommands();
			ArrayList<String> inMemory = new ArrayList<String>();
			Iterator<String> iter = commands.keySet().iterator();
			String key;
			while (iter.hasNext()) {
				key = iter.next();
				inMemory.add(key);
			}

			
			System.out.println(this.messages.get("msg_welcome"));
			while (true) {
				System.out.println(" ");
				System.out.println("You are at: " + Sys.getRoot());
				System.out.print(this.messages.get("msg_prompt"));
				String command = inputString();
				String[] exp = command.split(" ");
				if (inMemory.contains(exp[0])) {
					commands.get(exp[0]).run(command);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		try {
			Listener listener = new Listener();
			listener.init();
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}

	}

	private static String inputString() {
		BufferedReader bufferedreader = new BufferedReader(
				new InputStreamReader(System.in));
		String s = null;
		try {
			s = bufferedreader.readLine();
		} catch (IOException ioexception) {
			ioexception.printStackTrace();
		}
		return s;
	}
}
